package model

import (
	"fmt"
	"jingzhoulianxin/db"
	"time"
)

type tbUserAccountFlow struct {
	Id             int    `gorm:"primary_key;column:id" json:"id"`
	FlowId         int    `gorm:"column:flow_id" json:"FlowId"`
	Mobile         string `gorm:"column:mobile" json:"mobile"`
	Money          int    `gorm:"column:money" json:"money"`
	Cate           string `gorm:"column:cate" json:"cate"`
	Cash           int    `gorm:"column:cash" json:"cash"`
	Subsidy        int    `gorm:"column:subsidy" json:"subsidy"`
	CashBalance    int    `gorm:"column:cash_balance" json:"cashBalance"`
	SubsidyBalance int    `gorm:"column:subsidy_balance" json:"subsidyBalance"`
	Device         string `gorm:"column:device" json:"device"`
	CreateTime     time.Time `gorm:"column:create_time" json:"createTime"`
	Remark         string `gorm:"column:remark" json:"remark"`
}

func (this *tbUserAccountFlow) TableName() string {
	return "tb_user_account_flow"
}

func InsertData(accountFlow []accountFlowData) (err error){

	//TODO 当前使用版本不支持批量插入

	//tbUserAccountFlows := make([]tbUserAccountFlow, len(accountFlow))
	dbs := db.NetDb()

	for _, param := range accountFlow{

		tbUserAccount := tbUserAccountFlow{
			FlowId : param.Id,
			Mobile : param.Mobile,
			Money : param.Money,
			Cate : param.Cate,
			Cash : param.Cash,
			Subsidy : param.Subsidy,
			CashBalance : param.CashBalance,
			SubsidyBalance : param.SubsidyBalance,
			Device : param.DeviceSerial,
			CreateTime : param.CreateTime,
			Remark : param.Description,
		}

		err = dbs.Omit("Id").Create(&tbUserAccount).Error

	}
	//fmt.Println(tbUserAccountFlows)

	fmt.Println(err)
	defer dbs.Close()
	return
}

func GetMaxFlowId() (err error, accountFlow tbUserAccountFlow) {

	dbs := db.NetDb()
	dbs.Table("tb_user_account_flow").Select("Max(flow_id) as flow_id").First(&accountFlow)
	defer dbs.Close()
	return
}